public class BinarySearchTree {

    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val){
            this.val = val;
        }
    }

    public TreeNode root;//根节点

    //插入
    public void insert(int key){
        if (root ==null){
            root = new TreeNode(key);
            return;
        }

        TreeNode parent =null;
        TreeNode cur = root;
        TreeNode node = new TreeNode(key);

        while (cur != null){
            if (cur.val < key){
                parent = cur;
                cur = cur.right;
            }else if (cur.val > key){
                parent = cur;
                cur=cur.left;
            }else {
                //不能插入两个相同的数据
                return;
            }
        }

        if (parent.val > key){
            parent.left = node;
        }else {
            parent.right = node;
        }
    }

    /**
     * 时间复杂度：
     *        最好情况： O(logN)
     *        最坏情况： O(N)
     * @param key
     * @return
     */
    //查找  插入和删除都需要先查找是否存在这个数
    public TreeNode search(int key){
        TreeNode cur = root;
        while (cur != null){
            if (cur.val > key){
                cur = cur.left;
            }else if (cur.val == key){
                return cur;
            }else {
                cur = cur.right;
            }
        }
        return null;
    }

    //删除
    public void remove(int key){
        TreeNode cur = root;
        TreeNode parent = null;

        while (cur != null){
            if (cur.val > key){
                parent = cur;
                cur = cur.left;
            }else if (cur.val < key){
                parent = cur;
                cur = cur.right;
            }else {
                removeNode(parent,cur);
                return;
            }
        }
    }

    public void removeNode(TreeNode parent,TreeNode cur){
        if (cur.left == null){
            if (cur == root){
                root = cur.right;
            }else if (parent.left == cur){
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if (cur.right == null){
            if (cur == root){
                root = cur.left;
            }else if (parent.left == cur){
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            //替换删除
            //找到一个合适的数据 替换cur.val
            //左树找到最大值，也就是左树最右边的节点，最右边的节点的右树为空
            //右树找到最小值，也就是右树最左边的节点，最左边的节点的左树为空
            TreeNode targetParent = cur;
            TreeNode target = cur.right;//以右树为例
            while (target.left != null){
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if (targetParent.left == target){
                targetParent.left = target.right;
            }else {
                targetParent.right = target.right;
            }
        }
    }



    //https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5?tpId=13&&tqId=11179&rp=1&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking
    //将二叉搜索树转化为排序的双向链表
    public class Solution {
        TreeNode pre = null;
        TreeNode root = null;
        public TreeNode Convert(TreeNode pRootOfTree) {

            if (pRootOfTree == null) return null;
            Convert(pRootOfTree.left);
            if (root == null) {
                root = pRootOfTree;
            }
            if (pre != null) {
                pRootOfTree.left = pre;
                pre.right = pRootOfTree;
            }
            pre = pRootOfTree;
            Convert(pRootOfTree.right);
            return root;
        }

    }

}
